package core;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import bullets.BulletBoss;
import bullets.BulletEnemy;
import bullets.BulletWing;
import bullets.HeroBullet;
import enemy.EnemyBoss;
import hero.Hero;
import hero.UltimateSkill;
import hero.WingPlane;
import sky.Sky;

public class World extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static BufferedImage[] image = new BufferedImage[5];

	public static final int WORLD_WIDTH = 666;
	public static final int WORLD_HEIGHT = 998;
	public static final int RUN = 1;
	public static final int START = 2;
	public static final int END = 4;
	private int status = START;

	private int heroFlag;
	static {
		try {
			image[START] = ImageIO.read(new File("src/images/start.png"));
			image[END] = ImageIO.read(new File("src/images/gameover.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private ArrayList<FlyingObject> enemys = new ArrayList<>();
	private ArrayList<HeroBullet> bullets = new ArrayList<>();
	private ArrayList<BulletEnemy> bulletEnemies = new ArrayList<>();
	private ArrayList<BulletBoss> bulletBoss = new ArrayList<>();
	private ArrayList<EnemyBoss> enemysBoss = new ArrayList<>();
	private ArrayList<UltimateSkill> us = new ArrayList<>();
	private ArrayList<WingPlane> wps = new ArrayList<>();
	private ArrayList<BulletWing> wingBullets = new ArrayList<>();
	
	public ArrayList<BulletWing> getWingBullets() {
		return wingBullets;
	}

	public void setWingBullets(ArrayList<BulletWing> wingBullets) {
		this.wingBullets = wingBullets;
	}

	public ArrayList<WingPlane> getWps() {
		return wps;
	}

	public void setWps(ArrayList<WingPlane> wps) {
		this.wps = wps;
	}

	public ArrayList<UltimateSkill> getUs() {
		return us;
	}

	public void setUs(ArrayList<UltimateSkill> us) {
		this.us = us;
	}

	public ArrayList<EnemyBoss> getEnemysBoss() {
		return enemysBoss;
	}

	public void setEnemysBoss(ArrayList<EnemyBoss> enemysBoss) {
		this.enemysBoss = enemysBoss;
	}

	public ArrayList<BulletEnemy> getBulletEnemies() {
		return bulletEnemies;
	}

	public void setBulletEnemies(ArrayList<BulletEnemy> bulletEnemies) {
		this.bulletEnemies = bulletEnemies;
	}

	public ArrayList<BulletBoss> getBulletBoss() {
		return bulletBoss;
	}

	public void setBulletBoss(ArrayList<BulletBoss> bulletBoss) {
		this.bulletBoss = bulletBoss;
	}

	private Sky sky = new Sky(1);
	private ArrayList<Hero> hero = new ArrayList<>();
	// 当前的英雄机客户端英雄机
	private Hero thisHero = new Hero();

	// 绘画当前状态
	public void paint(Graphics g) {
		sky.paintImage(g);
		if (status == RUN) {
			if (thisHero.getStatus() != thisHero.getRemove()) {
				thisHero.paintImage(g);
			}
			for (Hero h : hero) {
				if (h.getStatus() != h.getRemove() && h.getFlag() != heroFlag) {
					h.paintImage(g);
				}
			}
			//System.out.println(wingBullets);
			for(BulletWing bw:wingBullets) {
				bw.paintImage(g);
			}
			for (EnemyBoss eb : enemysBoss) {
				eb.paintImage(g);
			}
			for (FlyingObject f : enemys) {
				f.paintImage(g);
			}
			for (HeroBullet b : bullets) {
				b.paintImage(g);
			}
			for (BulletEnemy b : bulletEnemies) {
				b.paintImage(g);
			}
			for (BulletBoss b : bulletBoss) {
				b.paintImage(g);
			}
			for (UltimateSkill u : us) {
				u.paintImage(g);
			}
		}
		if (status == START) {
			g.drawImage(image[START], 0, 0, null);

		}
		if (status == END) {
			g.drawImage(image[END], 0, 0, null);
		}
	}

	public void action() {
		KeyListener kl = new KeyAdapter() {// 键盘监听内部类
			public void keyPressed(KeyEvent e) {// 监听键盘按压的时刻
				super.keyPressed(e);
				int num = e.getKeyCode();
				if (num == KeyEvent.VK_F1) {
					if (us.size() < 1 && thisHero.isLife() && thisHero.getUltimateSkill() > 0) {
						thisHero.setUltimateSkillFlag(true);
					}
				}
			}
		};
		this.addKeyListener(kl);// 添加上述的键盘监听事件
		this.setFocusable(true);
		this.requestFocus();
		MouseAdapter ma = new MouseAdapter() {
			public void mouseMoved(MouseEvent e) {
				if (status == RUN) {
					int moveX = e.getX() - thisHero.width / 2;
					int moveY = e.getY() - thisHero.height / 2;
					thisHero.moveTo(moveX, moveY);
				}
			}
		};
		this.addMouseListener(ma);
		this.addMouseMotionListener(ma);
		repaint();
	}

	public ArrayList<FlyingObject> getEnemys() {
		return enemys;
	}

	public void setEnemys(ArrayList<FlyingObject> enemys) {
		this.enemys = enemys;
	}

	public ArrayList<HeroBullet> getBullets() {
		return bullets;
	}

	public void setBullets(ArrayList<HeroBullet> bullets) {
		this.bullets = bullets;
	}

	public void setHero(ArrayList<Hero> heros) {
		for (Hero h : heros) {
			// 判断读回来的英雄机和当前英雄机是否已经匹配过，都未做过匹配进行配置
			if (!h.isHasget() && !thisHero.isHasget()) {
				heroFlag = h.getFlag();
				h.setHasget(true);
				thisHero = h;
				break;
			}
			if (thisHero.getFlag() == h.getFlag()) {
				thisHero.setStatus(h.getStatus());
				thisHero.setLife(h.getLife());
				thisHero.setScore(h.getScore());
				thisHero.setUltimateSkill(h.getUltimateSkill());
				//thisHero.setUltimateSkillFlag(h.isUltimateSkillFlag());
			}
		}
		hero = heros;
	}

	// 得到当前英雄机的信息
	public Hero getHeroinfo() {
		return thisHero;
	}

	public Sky getSky() {
		return sky;
	}

	public void setSky(Sky sky) {
		this.sky = sky;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}
